<template>
  <div class="login-page">
  <!-- 导航条 -->
  <van-nav-bar  title="面经登录"/>
  <!-- 中间表单 -->
  <van-form @submit="onSubmit">
  <van-field  name="username" v-model="username"  label="用户名"  placeholder="请输入用户名"
  :rules="[
    {required:true,message:'请填写用户名'},
    {pattern:/^\w{5,}$/,message:'用户名至少包含5个字符'}
  ]"
  />
  <van-field  type="password"  name="password"  v-model="password" label="密码"  placeholder="请输入密码"
  :rules="[
    {required:true,message:'请填写密码'},
    {pattern:/^\w{6,}$/,message:'用户名至少包含6个字符'}
  ]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">登录</van-button>
  </div>
</van-form>
<!-- 注册 -->
<router-link class="link" to="/register">注册账号</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (value) {
      // 1.提示
      this.$toast.loading('正在登录中...')
      // 2.调用接口
      const res = await login(value)
      this.$toast.success('登陆成功~')
      console.log(res)
      // 3.存储数据和跳转
      // localStorage.setItem('hm-vant-h5-token', res.data.token)
      // this.$router.replace('/')
      setToken(res.data.token)
      this.$router.replace('/')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
